package com.xin.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xin.entity.*;
import com.xin.service.*;
import com.xin.util.DecryptionUtil;
import com.xin.util.ReflectUtil;
import com.xin.util.TeacherLevel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 学生信息 前端控制器
 * </p>
 *
 * @author l1
 * @since 2017-11-14
 */
@Controller
@RequestMapping("/student")
public class UStudentInfoController {

    @Value("${student.common.key}")
    private String studentCommonKey;
    @Value("${student.common.secret}")
    private String studentCommonSecret;
    @Value("${student.image.key}")
    private String studentImageKey;
    @Value("${student.image.secret}")
    private String studentImageSecret;

    @Autowired
    private STeacherResourcesService sTeacherResourcesService;

    @Autowired
    private UTeacherInfoService uTeacherInfoService;

    @Autowired
    private UStudentInfoService uStudentInfoService;

    @Autowired
    private SDeptProfessionalService sDeptProfessionalService;

    @Autowired
    private SRoleOrgService sRoleOrgService;

    @GetMapping("")
    public String index(String account_id, Model model, HttpServletRequest request){
        String id ;
        if(account_id.length()>10){
            id = DecryptionUtil.desc(account_id, studentCommonKey,studentCommonSecret);
        }else{
            id = account_id;
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("id",id);
        //查询人员角色
//        STeacherResources sTeacherResources = sTeacherResourcesService.selectOne(new EntityWrapper<STeacherResources>().eq("teacher_id", id));
//        if(sTeacherResources==null) return "notRole";
//        Integer level = sTeacherResources.getLevel();
        UTeacherInfo utInfo = uTeacherInfoService.selectOne(new EntityWrapper<UTeacherInfo>().eq("id", id));

        if(utInfo==null) return "notExit";

        List<SOrg> orgs = sRoleOrgService.findOrgsByTeacherId(id);
        if(orgs==null || orgs.isEmpty()) {
            model.addAttribute("name",utInfo.getName());
            uTeacherInfoService.findDetail(id,model);
            return "teacherDetail";
        }

//        session.setAttribute("resource",sTeacherResources);
//        session.setAttribute("level",level);
//        String college = sTeacherResources.getSubjectAttribution() == null ? utInfo.getSubjectAttribution() : sTeacherResources.getSubjectAttribution();
//        session.setAttribute("college",college);
//        String profession = sTeacherResources.getPrefessionalAttribution() == null ? utInfo.getPrefessionalAttribution(): sTeacherResources.getPrefessionalAttribution();
//        session.setAttribute("profession",profession);
        session.setAttribute("utInfo",utInfo);
        session.setAttribute("teacherName",utInfo.getName());

//        model.addAttribute("level",level);
        model.addAttribute("name",utInfo.getName());
        uStudentInfoService.index(model,orgs);

        if(model.containsAttribute("studentDetail")){
            model.addAttribute("name",utInfo.getName());
            uTeacherInfoService.findDetail(id,model);
            return "teacherDetail";
        }else {
//        System.out.println("model:"+ JSON.toJSONString(model));
            return "student";
        }
    }


    //检索
    @GetMapping("/list")
    public String list(Model model,HttpServletRequest request,String statName){
        HttpSession session = request.getSession(true);
//        Integer level = (Integer)session.getAttribute("level");
        String name = (String) session.getAttribute("name");

        UStudentInfo uStudentInfo = new UStudentInfo();
        if(StringUtils.isNotEmpty(statName)){
//            if(level == TeacherLevel.ONE.getLevel()){
                uStudentInfo.setSubjectAttribution(statName);
//            }else {
//                uStudentInfo.setPrefessionalAttribution(statName);
//            }
        }
        SDeptProfessional sdp = new SDeptProfessional();
//        if(level == TeacherLevel.ONE.getLevel()){
        //学院
        sdp.setFlag("1");
        List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
                new EntityWrapper<>(sdp).orderBy("show_order", true));
        model.addAttribute("colleges",colleges);
        //专业
        sdp.setFlag("2");
        List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
                new EntityWrapper<>(sdp).orderBy("show_order", true));
        model.addAttribute("professionls",professionls);
        //list
        PageEntity pageEntity = new PageEntity();

        Page<UStudentInfo> results =uStudentInfoService.selectPage(
                        new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
                        new EntityWrapper<>(uStudentInfo));

        model.addAttribute("results",results);
        model.addAttribute("uStudentInfo",uStudentInfo);
        model.addAttribute("pageEntity",new PageEntity());
        model.addAttribute("name",name);

//        }else if(level == TeacherLevel.TWO.getLevel()){
//            //院系
//            //学院
//            sdp.setFlag("1");
//            sdp.setName(college);
//            List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("colleges",colleges);
//            //专业
//            SDeptProfessional sDeptProfessional = sDeptProfessionalService.selectOne(new EntityWrapper<SDeptProfessional>().eq("name", college));
//            sdp.setParentId(sDeptProfessional.getId());
//            List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("professionls",professionls);
//
//            //list
//            PageEntity pageEntity = new PageEntity();
////            UStudentInfo uStudentInfo = new UStudentInfo();
//            uStudentInfo.setSubjectAttribution(college);
//            Page<UStudentInfo> results = uStudentInfoService.selectPage(
//                    new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
//                    new EntityWrapper<>(uStudentInfo));
//
//            model.addAttribute("results",results);
//            model.addAttribute("uStudentInfo",uStudentInfo);
//            model.addAttribute("pageEntity",new PageEntity());
//            model.addAttribute("name",name);
//        }else if(level == TeacherLevel.THREE.getLevel()){
//            //专业
//            //学院
//            sdp.setFlag("1");
//            sdp.setName(college);
//            List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("colleges",colleges);
//            //专业
//            sdp.setFlag("2");
//            sdp.setName(profession);
//            List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("professionls",professionls);
//
//            //list
//            PageEntity pageEntity = new PageEntity();
////            UStudentInfo uStudentInfo = new UStudentInfo();
//            uStudentInfo.setSubjectAttribution(college);
//            uStudentInfo.setPrefessionalAttribution(profession);
//            Page<UStudentInfo> results = uStudentInfoService.selectPage(
//                    new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
//                    new EntityWrapper<>(uStudentInfo));
//
//            model.addAttribute("results",results);
//            model.addAttribute("uStudentInfo",uStudentInfo);
//            model.addAttribute("pageEntity",new PageEntity());
//            model.addAttribute("name",name);
//        }

        return "studentList";
    }

    @PostMapping("/listByPage")
    public String listByPage(Model model,HttpServletRequest request,PageEntity pageEntity,UStudentInfo uStudentInfo){
        if(uStudentInfo == null){ uStudentInfo = new UStudentInfo();}
        ReflectUtil.emptyToNull(uStudentInfo);
        HttpSession session = request.getSession(true);
//        Integer level = (Integer)session.getAttribute("level");
//        String college = (String) session.getAttribute("college");
//        String profession = (String) session.getAttribute("profession");
        String name = (String) session.getAttribute("name");

        SDeptProfessional sdp = new SDeptProfessional();
//        if(level == TeacherLevel.ONE.getLevel()){
        //学院
        sdp.setFlag("1");
        List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
                new EntityWrapper<>(sdp).orderBy("show_order", true));
        model.addAttribute("colleges",colleges);
        //专业
        sdp.setFlag("2");
        List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
                new EntityWrapper<>(sdp).orderBy("show_order", true));
        model.addAttribute("professionls",professionls);
        //list
        Page<UStudentInfo> results =uStudentInfoService.selectPage(
                new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
                new EntityWrapper<>(uStudentInfo));

        model.addAttribute("results",results);
        model.addAttribute("uStudentInfo",uStudentInfo);
        model.addAttribute("pageEntity",new PageEntity());
        model.addAttribute("name",name);

//        }else if(level == TeacherLevel.TWO.getLevel()){
//            //院系
//            //学院
//            sdp.setFlag("1");
//            sdp.setName(college);
//            List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("colleges",colleges);
//            //专业
//            SDeptProfessional sDeptProfessional = sDeptProfessionalService.selectOne(new EntityWrapper<SDeptProfessional>().eq("name", college));
//            sdp.setParentId(sDeptProfessional.getId());
//            List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("professionls",professionls);
//
//            //list
//            uStudentInfo.setSubjectAttribution(college);
//            Page<UStudentInfo> results = uStudentInfoService.selectPage(
//                    new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
//                    new EntityWrapper<>(uStudentInfo));
//
//            model.addAttribute("results",results);
//            model.addAttribute("uStudentInfo",uStudentInfo);
//            model.addAttribute("pageEntity",new PageEntity());
//            model.addAttribute("name",name);
//
//        }else if(level == TeacherLevel.THREE.getLevel()){
//            //专业
//            //学院
//            sdp.setFlag("1");
//            sdp.setName(college);
//            List<SDeptProfessional> colleges = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("colleges",colleges);
//            //专业
//            sdp.setFlag("2");
//            sdp.setName(profession);
//            List<SDeptProfessional> professionls = sDeptProfessionalService.selectList(
//                    new EntityWrapper<>(sdp).orderBy("show_order", true));
//            model.addAttribute("professionls",professionls);
//
//            //list
//            uStudentInfo.setSubjectAttribution(college);
//            uStudentInfo.setPrefessionalAttribution(profession);
//            Page<UStudentInfo> results = uStudentInfoService.selectPage(
//                    new Page<UStudentInfo>(pageEntity.getPageNum(), pageEntity.getPageSize()),
//                    new EntityWrapper<>(uStudentInfo));
//
//            model.addAttribute("results",results);
//            model.addAttribute("uStudentInfo",uStudentInfo);
//            model.addAttribute("pageEntity",new PageEntity());
//            model.addAttribute("name",name);
//        }

        return "studentList";
    }

    /**
     * 查询人员信息
     *
     */
    @GetMapping("/findDetail/{id}")
    public String findDetail(@PathVariable("id") String id,Model model,HttpServletRequest request){

        HttpSession session = request.getSession(true);
        String name = (String) session.getAttribute("name");

        model.addAttribute("name",name);
        uStudentInfoService.findDetail(id,model);
        return "studentDetail";
    }

    /**
     * 查询人员信息
     *
     */
    @GetMapping("/detail")
    public String detail(String account_id,Model model,HttpServletRequest request){
        String id ;
        if(account_id.length()>10){
            id = DecryptionUtil.desc(account_id, studentImageKey,studentImageSecret);
        }else{
            id = account_id;
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("id",id);
        //查询人员角色
        UStudentInfo utInfo = uStudentInfoService.selectOne(new EntityWrapper<UStudentInfo>().eq("id", id));

        if(utInfo==null) return "notExit";
        String college = utInfo.getSubjectAttribution();
        session.setAttribute("college",college);
        String profession = utInfo.getPrefessionalAttribution();
        session.setAttribute("profession",profession);
        session.setAttribute("utInfo",utInfo);
        session.setAttribute("teacherName",utInfo.getName());

        model.addAttribute("name",utInfo.getName());
        uStudentInfoService.findDetail(id,model);
        return "studentDetail";
    }

}
